package com.cdj.common.view;

import android.content.Context;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.util.List;

/**
 * ---------------------------------------------------------<br />
 * desc：<br />
 * author：陈德基 <br />
 * date：2021/7/15 9:20<br />
 * email：18701434169@163.com<br />
 * qq: 781571323
 * wx: melody_2009
 * ---------------------------------------------------------<br />
 */
public class CommonMutiTypeListView<T extends MultiItemEntity> extends RecyclerView implements IAdapterHolder<T>{
    private BaseMultiItemQuickAdapter<T, BaseViewHolder> adapter;

    public CommonMutiTypeListView(@NonNull Context context) {
        this(context, null);
    }

    public CommonMutiTypeListView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CommonMutiTypeListView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setLayoutManager(new LinearLayoutManager(context));
    }

    public static class ItemTypeLayout {
        int type;
        int layoutID;

        public ItemTypeLayout(int type, int layoutID) {
            this.type = type;
            this.layoutID = layoutID;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public int getLayoutID() {
            return layoutID;
        }

        public void setLayoutID(int layoutID) {
            this.layoutID = layoutID;
        }
    }

    public void setMutiAdapter(CommonMutiTypeAdapter adapter) {
        this.adapter = adapter;
//        View emptyView = LayoutInflater.from(getContext()).inflate(R.layout.empty_layout, null, false);
//        adapter.setEmptyView(emptyView);
        setAdapter(this.adapter);
    }

    public static class CommonMutiTypeAdapter<T extends MultiItemEntity> extends BaseMultiItemQuickAdapter<T, BaseViewHolder> {

        private IAdapterChildData<T> adapterChildData;

        public void setAdapterChildData(IAdapterChildData<T> adapterChildData) {
            this.adapterChildData = adapterChildData;
        }
        public CommonMutiTypeAdapter(List<ItemTypeLayout> itemTypeLayouts) {
            super(null);
            if (itemTypeLayouts != null) {
                for (int i = 0; i < itemTypeLayouts.size(); i++) {
                    ItemTypeLayout itemTypeLayout = itemTypeLayouts.get(i);
                    addItemType(itemTypeLayout.type, itemTypeLayout.layoutID);
                }
            }
        }

        @Override
        protected void convert(BaseViewHolder helper, T item) {
            if (adapterChildData != null) {
                adapterChildData.fillDataToView(helper, item);
            }
        }
    }

    @Override
    public BaseQuickAdapter<T, BaseViewHolder> adapter() {
        return adapter;
    }
}
